/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package group6renderer.renderernode;

import group6renderer.Display;
import group6renderer.Group6Renderer;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/** ClASS NAME: ImageNode
 * DESCRIPTION: An class to deal with images
 * COLABORATORING CLASSES: LeafNode, Display, Exception, IOException
 * @author jonnie
 */
public class ImageNode extends LeafNode
{
    private String path;
    private BufferedImage image;


    
    /** 
     * A method overriding the abstract method in TreeNode. It adjusts the properties of the State class according to the type of node
     * concerned.  
     * @param disp the Display object of the project (there is only one)
     * @Override
     */
    public void drawNode(Display disp)
    {
	
	if(image != null)
	{
	    disp.addImage(image);
	}
    }

    /**
     * A method to return whether this Node has a closing tag
     * @Override
     * @return true if this node has a closing tag and false otherwise
     */
    public boolean hasClosing()
    {
	return false;
    }

    /** 
     * A method to return a new copy of this node.
     * @Override
     * @return a new instance of this node with the original name
     */
    public TreeNode copy()
    {
	return new ImageNode();
    }
    
    /**
     * A method to set the path of an image
     * @param path the path to give the image
     */
    public void setPath(String path) throws Exception
    {
	File theImage = new File(path);
	if(!theImage.isAbsolute())
	{
	    theImage = new File(Group6Renderer.htmlFile.getParent(), theImage.getPath());
	}
	if(!(theImage.getName().endsWith(".jpg") || theImage.getName().endsWith(".gif") || theImage.getName().endsWith(".png")) )
	{
	    throw new Exception("Not An Allowed Image Type: " + theImage.getPath());
	}
	try
	{
		image = ImageIO.read(theImage);
	}
	catch (IOException ex)
	{
		throw new Exception("Image File Not Found in the indexed location: " + theImage.getPath());
	}
    }
    
}
